package com.xdtech.game.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.PageRequest;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.game.dao.RankDao;
import com.xdtech.game.model.Rank;
import com.xdtech.game.service.RankService;
import com.xdtech.game.vo.RankItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-16 23:17:03
 * @since 1.0
 * @see
 */
@Service
public class RankServiceImpl implements RankService {
	@Autowired
	private RankDao rankDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param entity
	 */
	public void save(Rank entity) {
		rankDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Rank> entities) {
		rankDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param entity
	 */
	public void delete(Rank entity) {
		rankDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		rankDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param id
	 * @return
	 */
	public Rank get(Long id) {
		return rankDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @return
	 */
	public List<Rank> getAll() {
		return rankDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> ranks = null;
		long rows = 0;
		if (pg!=null) {
			Page<Rank> page = rankDao.findPage(pg,"from Rank order by createTime desc", values);
			ranks = BeanUtils.copyListProperties(
					RankItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Rank> rankList = rankDao.find("from Rank order by id desc", values);
			ranks = BeanUtils.copyListProperties(
					RankItem.class, rankList);
			rows = ranks.size();
		}
		results.put("total", rows);
		results.put("rows", ranks);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateRank(RankItem item) {
		Rank rank = null;
		if (item.getId()==null) {
			rank = new Rank();
		}else {
			rank = rankDao.get(item.getId());
		}
		//复制前台修改的属�?
		BeanUtils.copyProperties(rank, item);
		rankDao.save(rank);
		item.setId(rank.getId());
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param rankId
	 * @return
	 */
	public RankItem loadRankItem(Long rankId) {
		Rank rank = rankDao.get(rankId);
		RankItem rankItem = new RankItem();
		BeanUtils.copyProperties(rankItem, rank);
		return rankItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteRankInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-16 23:17:03
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteRankInfo(List<Long> rankIds) {
		for (Long id : rankIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-16下午11:18:25
	 * @modified by
	 * @param gameCode
	 * @param topNum
	 * @return
	 */
	@Override
	public Map<String, Object> loadTopRank(String gameCode, int topNum) {
		Page<Rank> ranks = rankDao.findPage(new PageRequest(1, topNum), "from Rank where gameCode=? order by score desc", gameCode);
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("data", RankItem.createItems(ranks.getResult()));
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:25
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-5-13下午11:07:45
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public int getSort(RankItem item) {
		return rankDao.getRank(item.getGameCode(),item.getId());
	}

}
